import unittest
from time import sleep
from Page.my_order_page import *
from common.databaseTool import DatabaseTool


class MyOrderCase(unittest.TestCase):
    def setUp(self):
        self.driver = open_op_browser(path)
        self.mop = MyOrderPage(self.driver)
        self.mop.open_url(login_url)

    @classmethod
    def setUpClass(cls):
        # 连接数据库
        cls.db = DatabaseTool("168168", "ecshop_db", port=3308)
        # sql = "delete from ecs_order_info where user_id =109"
        # cls.db.execute_one(sql)
        #
        # # 重新添加意见商品进入购物车

    def tearDown(self):
        self.mop.close()

    def test_my_order_case01(self):
        """
        验证查询订单信息项显示正确 - 订单号
        :return:
        """
        # 进入我的订单页面
        self.mop.my_order()
        # 获取页面内的订单单号
        num = self.mop.get_order_text()
        sql = "select order_sn from ecs_order_info where consignee = '王小明'"
        order_sn = str(self.db.fetchOne(sql)["order_sn"])
        self.assertEqual(num, order_sn, msg="订单显示不正确")

    def test_my_order_case02(self):
        """
        验证查询订单信息项显示正确 - 下单时间
        :return:
        """
        # 进入我的订单页面
        self.mop.my_order()
        # 获取到我的订单的时间
        times = self.mop.get_time_text()
        sql = "select add_time from ecs_order_info where consignee = '王小明'"
        import time
        # 将获取到的时间戳转换成指定格式
        add_time = time.strftime("%Y-%m-%d", time.localtime(int(self.db.fetchOne(sql)["add_time"])))
        self.assertIn(add_time, times, msg="用例执行失败 时间展示不正确")

    def test_my_order_case03(self):
        """
        验证查询订单信息项显示正确 - 订单状态
        :return:
        """
        # 进入我的订单页面
        self.mop.my_order()
        # 获取订单 状态
        sta = self.mop.get_status_text()
        sql = "select order_status from ecs_order_info where consignee = '王小明'"
        order_status = str(self.db.fetchOne(sql)["order_status"])
        if order_status == "0":
            self.order = "未确认"
        elif order_status == "1":
            self.order = "已确认"
        elif order_status == "2":
            self.order = "合并"
        elif order_status == "3":
            self.order = "取消"
        elif order_status == "4":
            self.order = "无效"
        elif order_status == "5":
            self.order = "退货"
        else:
            print("订单状态有误")

        self.assertIn(self.order,sta, msg="用例执行失败 订单状态展示不正确")

    def test_my_order_case04(self):
        """
        验证查询订单信息项显示正确 - 款项状态
        :return:
        """
        self.mop.my_order()
        # 获取订单 状态
        sta = self.mop.get_status_text()
        sql = "select pay_status from ecs_order_info where consignee = '王小明'"
        pay_status = str(self.db.fetchOne(sql)["pay_status"])
        if pay_status == "0":
            self.order = "未付款"
        elif pay_status == "1":
            self.order = "付款中"
        elif pay_status == "2":
            self.order = "已付款"
        else:
            print("付款状态有误")
        self.assertIn(self.order, sta, msg="用例执行失败 款项状态展示不正确")

    def test_my_order_case05(self):
        """
        验证查询订单信息项显示正确 - 配送状态
        :return:
        """
        self.mop.my_order()
        # 获取订单 状态
        sta = self.mop.get_status_text()
        sql = "select shipping_status from ecs_order_info where consignee = '王小明'"
        shipping_status = str(self.db.fetchOne(sql)["shipping_status"])
        if shipping_status == "0":
            self.order = "未发货"
        elif shipping_status == "1":
            self.order = "已发货"
        elif shipping_status == "2":
            self.order = "确认收货"
        elif shipping_status == "3":
            self.order ="备货中"
        elif shipping_status== "4":
            self.order == "已发货(部分商品)"
        else:
            print("付款状态有误")
        self.assertIn(self.order, sta, msg="用例执行失败 配送状态展示不正确")

    def test_my_order_case06(self):
        """
        验证取消订单 按钮可用
        :return:
        """
        # 打开我的订单页面
        self.mop.my_order()
        # 点击取消订单
        self.mop.cancel_order()
        # 获取弹窗信息
        try:
            al = self.driver.switch_to.alert
            self.assertIn("您确认要取消该订单吗？", al.text, msg="用例执行失败,未捕获到取消弹框按钮")
            # 点击取消按钮
            al.dismiss()
        except Exception as e:
            raise ("用例执行失败")

    def test_my_order_case07(self):
        """
        验证再次购买按钮可用
        :return:
        """
        # 打开我的订单页面
        self.mop.my_order()
        # 点击再次购买按钮
        self.mop.bought_again()
        sleep(10)
        url = self.driver.current_url
        print(url)
        self.assertIn("step=cart&type=5", url, msg="用例执行失败重新购买失败 商品加入购物车失败")

    def test_my_order_case08(self):
        """
        验证取消订单
        :return:
        """
        # 打开我的订单页面
        self.mop.my_order()
        # 点击取消订单
        self.mop.cancel_order()
        # 获取弹窗信息
        try:
            al = self.driver.switch_to.alert
            self.assertIn("您确认要取消该订单吗？", al.text, msg="用例执行失败,未捕获到取消弹框按钮")
            # 点击确认按钮
            al.accept()
            sleep(10)
            # 获取数据库内该订单的状态
            sql = "select order_status from ecs_order_info where consignee = '王小明'"
            order_status = str(self.db.fetchOne(sql)["order_status"])
            print(order_status)
            self.assertEqual(order_status, "2", msg="用例执行失败 订单状态展示不正确")
            # 恢复数据
        except Exception as e:
            raise ("用例执行失败")

    def test_my_order_case09(self):
        """
        验证查看订单详情链接可用
        :return:
        """
        # 打开我的订单页面
        self.mop.my_order()
        # 点击订单号进入订单详情页面
        self.mop.my_order()
        sleep(10)
        url = self.driver.current_url
        print(url)
        self.assertIn("act=order_list", url, msg="用例执行成功,进入查看订单详情页面")


if __name__ == '__main__':
    unittest.main()
